package cn.stylefeng.guns.modular.bms.service.impl;

import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.bms.entity.BmsWelfare;
import cn.stylefeng.guns.modular.bms.mapper.BmsWelfareMapper;
import cn.stylefeng.guns.modular.bms.model.params.BmsWelfareParam;
import cn.stylefeng.guns.modular.bms.model.result.BmsWelfareResult;
import  cn.stylefeng.guns.modular.bms.service.BmsWelfareService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 福利信息表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2021-03-22
 */
@Service
public class BmsWelfareServiceImpl extends ServiceImpl<BmsWelfareMapper, BmsWelfare> implements BmsWelfareService {

    @Override
    public void add(BmsWelfareParam param){
        BmsWelfare entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BmsWelfareParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BmsWelfareParam param){
        BmsWelfare oldEntity = getOldEntity(param);
        BmsWelfare newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BmsWelfareResult findBySpec(BmsWelfareParam param){
        return null;
    }

    @Override
    public List<BmsWelfareResult> findListBySpec(BmsWelfareParam param){
        return baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(BmsWelfareParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<ZTreeNode> welfareTreeList() {
        return baseMapper.welfareTreeList();
    }

    private Serializable getKey(BmsWelfareParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BmsWelfare getOldEntity(BmsWelfareParam param) {
        return this.getById(getKey(param));
    }

    private BmsWelfare getEntity(BmsWelfareParam param) {
        BmsWelfare entity = new BmsWelfare();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
